import time
import datetime
import requests
import pymysql
from xml.etree import ElementTree
from bs4 import BeautifulSoup
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler


def BlockingScreen():
    scheduler = BlockingScheduler()
    scheduler.add_job(test, 'cron', hour='*', minute='*', second='0')
    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()
    except Exception as e:
        print(e)


def test(qq_str):
    url_str = 'http://www.webxml.com.cn//webservices//qqOnlineWebService.asmx//qqCheckOnline?qqCode=%s' % qq_str
    text_str = requests.get(url_str)
    text_str.encoding = 'utf-8'
    # 解析xml格式内容，将字符串转为特殊的对象
    node = ElementTree.XML(text_str.text)
    if node.text == 'Y':
        print('QQ:{} 在线'.format(qq_str))
    else:
        print('QQ:{} 离线'.format(qq_str))
    return


def get_db():
    db = pymysql.connect(host='127.0.0.1', port=3306, user="root", passwd="root", db="test", charset="utf8",
                         cursorclass=pymysql.cursors.DictCursor)
    db.set_charset('utf8')
    return db


def get_xml_data():
    db = get_db()
    try:
        with db.cursor() as cursor:
            _time = int(time.time())
            datas = {}
            _sql = 'insert into `qiao_xml_data` (`c_id`,`xml_id`,`data`,`time`) values (%s,%s,%s,%s)'
            # cursor.execute(_sql)
            cursor.executemany(_sql, [(k, '', v, _time) for k, v in datas.items()])
            db.commit()

    except Exception as e:
        print('db error:', e)
        db.rollback()
    finally:
        db.close()



if __name__ == '__main__':
    BlockingScreen()
    pass
